<?php


namespace app\model;

use think\Model;

class AdminUsersRole extends Model
{
    public $adminId;
    public $roleId;

    private $adminRole;
    private $data;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->adminRole = new AdminRole();
        $this->data = array();
    }

    public function __destruct()
    {
        parent::__destruct();
        unset(
            $this->adminRole,
            $this->data
        );
    }

    public function getUsersRole()
    {
        return $this->where('admin_id', $this->adminId)
            ->field('admin_id, role_id')
            ->select()->each(
                function ($item)
                {
                    $this->adminRole->roleId = $item['role_id'];
                    $item['role_list'] = $this->adminRole->queryUsersRole();
                }
            )->toArray();
    }

    public function addAdminUserRole()
    {
        $this->data['admin_id'] = $this->adminId;
        $this->data['role_id'] = $this->roleId;
        return $this->save($this->data);
    }

    public function editAdminUserRole()
    {
        $this->data['role_id'] = $this->roleId;
        return $this->where('admin_id', $this->adminId)
            ->save($this->data);
    }
}